package com.ruoyi.order.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.RemoteCheServiceSort;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.order.domain.*;
import com.ruoyi.order.domain.*;
import com.ruoyi.order.service.ICheOrderAndUserService;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.order.api.RemoteCarNameService;
import com.ruoyi.order.api.RemoteCarTypeService;
import com.ruoyi.order.api.RemoteUserService;
import com.ruoyi.order.api.domain.CheCarName;
import com.ruoyi.order.api.domain.CheCarType;
import com.ruoyi.shop.api.CheApiShopService;
import com.ruoyi.util.Result;
import com.ruoyi.order.service.ICheOrderUserShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.order.service.ICheOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/order")
//跨域
@CrossOrigin
public class CheOrderController extends BaseController
{
    @Autowired
    private CheApiShopService shopService;
    @Autowired
    private ICheOrderService cheOrderService;
    @Autowired
    private RemoteCarNameService remoteCheService;
    @Autowired
    private RemoteCarTypeService remoteCarTypeService;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private ICheOrderAndUserService cheOrderAndUserService;
    @Autowired
    private RemoteCheServiceSort serviceSort;

    //
    @Autowired
    private ICheOrderUserShopService cheOrderUserShopService;

    /**
     * 查询订单列表
     */
//    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(CheOrder cheOrder)
    {
        startPage();
        List<CheOrder> list = cheOrderService.selectCheOrderList(cheOrder);
        //封装服务项目类别
        List sortList = new ArrayList();
        Map<String,Object> sortMap = new HashMap();
        for (CheOrder co : list){
            //获取车辆类型
            AjaxResult resultType = remoteCarTypeService.getInfo(co.getCarTypeId());
            LinkedHashMap typeMap = (LinkedHashMap) resultType.get("data");
            CheCarType cheCarType = BeanUtil.mapToBean(typeMap, CheCarType.class, true);
            co.setCarType(cheCarType.getName());
            //获取车辆名称
            AjaxResult cheName = remoteCheService.getInfo(co.getCarId());
            LinkedHashMap cheData = (LinkedHashMap) cheName.get("data");
            CheCarName carName = BeanUtil.mapToBean(cheData, CheCarName.class, true);
            co.setCarName(carName.getName());

            //获取用户名称
            AjaxResult info = remoteUserService.getInfo(co.getUserId());
            LinkedHashMap map = (LinkedHashMap) info.get("data");
            CheUser cheUser = BeanUtil.mapToBean(map, CheUser.class, true);
            //查询店铺名称
            AjaxResult shopServiceInfo = shopService.getInfo(co.getShopId(), SecurityConstants.INNER);
            LinkedHashMap linkedHashMap = (LinkedHashMap) shopServiceInfo.get("data");
            CheShop cheShop = BeanUtil.mapToBean(linkedHashMap, CheShop.class, true);
            co.setShopName(cheShop.getShopName());
            //查询订单中的服务项目
            List<CheServiceName> list1 = cheOrderService.selectAllByOrderId2(co.getId());
            co.setCheServiceNames(list1);
            //查询订单中服务项目的种类
            for (CheServiceName cheServiceName : list1){
                AjaxResult info1 = serviceSort.getInfo(cheServiceName.getServiceSortId());
                LinkedHashMap data = (LinkedHashMap) info1.get("data");
                CheServiceSort cheServiceSort = BeanUtil.mapToBean(data, CheServiceSort.class, true);
                sortMap.put("serviceSortName",cheServiceSort.getName());
                sortMap.put("price",cheServiceSort.getPrice());
                sortList.add(sortMap);
            }
            co.setUserName(cheUser.getRealName());
            co.setServiceSorts(sortList);
        }
        return getDataTable(list);
    }

//    @RequiresPermissions("order:order:list")
    @GetMapping("/list1")
    public TableDataInfo list1(CheOrder cheOrder) {
        startPage();
        cheOrder.setType(1L);
        List<CheOrder1> list = cheOrderAndUserService.selectCheOrderList(cheOrder);
        return getDataTable(list);
    }
    @RequiresPermissions("order:order:list")
    @GetMapping("/list2")
    public TableDataInfo list2(CheOrder cheOrder)  {
        startPage();
        cheOrder.setType(2L);
        List<CheOrder1> list = cheOrderAndUserService.selectCheOrderList2(cheOrder);
        return getDataTable(list);
    }
    @RequiresPermissions("order:order:list")
    @GetMapping("/list3")
    public TableDataInfo list3(CheOrder cheOrder) {
        startPage();
        cheOrder.setType(3L);
        List<CheOrder1> list = cheOrderAndUserService.selectCheOrderList3(cheOrder);
        return getDataTable(list);
    }
    @RequiresPermissions("order:order:list")
    @GetMapping("/list4")
    public TableDataInfo list4(CheEvaluate cheEvaluate) {
        startPage();
        List<CheEvaluate> list = cheOrderAndUserService.selectCheOrderList4(cheEvaluate);
        return getDataTable(list);
    }
    //1:用户端 查询全部
//    @RequiresPermissions("order:order:list")
    @GetMapping("/listn")
    public TableDataInfo listUser(CheOrder cheOrder) {
        startPage();
        cheOrder.setType(3L);
        List<CheOrderUserShop> list = cheOrderUserShopService.selectCheOrderList(cheOrder);
        return getDataTable(list);
    }
    //2:用户端根据id查询
    @GetMapping(value = "/byId/{id}")
    public AjaxResult getById(@PathVariable("id") Long id)
    {
        return success(cheOrderUserShopService.selectCheOrderById(id));
    }
    //3 待支付---没成功
//    @GetMapping("/listNoPay")
//    public TableDataInfo listUserInfo(CheOrder cheOrder) {
//        startPage();
//        cheOrder.setType(3L);
//        List<CheOrderUserShop> list = cheOrderUserShopService.selectOrderByNoPay(cheOrder);
//        return getDataTable(list);
//    }



    /**
     * 导出订单列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheOrder cheOrder)
    {
        List<CheOrder> list = cheOrderService.selectCheOrderList(cheOrder);
        ExcelUtil<CheOrder> util = new ExcelUtil<CheOrder>(CheOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
//    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        CheOrder cheOrder = cheOrderService.selectCheOrderById(id);
        //查询所有订单id
        List<CheServiceName> list1 = cheOrderService.selectAllByOrderId2(id);
        //获取车辆名称
        AjaxResult info = remoteCheService.getInfo(cheOrder.getCarId());
        LinkedHashMap data = (LinkedHashMap) info.get("data");
        CheCarName carName = BeanUtil.mapToBean(data, CheCarName.class, true);
        cheOrder.setCarName(carName.getName());
        //查询服务项目类别
        List sortList = new ArrayList();
        Map<String,Object> map = new HashMap();
        for (CheServiceName cheServiceName : list1){
            System.out.println(cheServiceName);
            AjaxResult serviceSortInfo = serviceSort.getInfo(cheServiceName.getServiceSortId());
            LinkedHashMap data1 = (LinkedHashMap) serviceSortInfo.get("data");
            CheServiceSort cheServiceSort = BeanUtil.mapToBean(data1, CheServiceSort.class, true);
            map.put("serviceSortName",cheServiceSort.getName());
            map.put("price",cheServiceSort.getPrice());
            sortList.add(map);
        }
        cheOrder.setServiceSorts(sortList);
        //获取车辆类型
        System.out.println(cheOrder.getCarTypeId());
        AjaxResult resultType = remoteCarTypeService.getInfo(cheOrder.getCarTypeId());
        LinkedHashMap typeMap = (LinkedHashMap) resultType.get("data");
        CheCarType cheCarType = BeanUtil.mapToBean(typeMap, CheCarType.class, true);
        cheOrder.setCarType(cheCarType.getName());
        return success(cheOrder);
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheOrder cheOrder)
    {
        return toAjax(cheOrderService.insertCheOrder(cheOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheOrder cheOrder)
    {
        return toAjax(cheOrderService.updateCheOrder(cheOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheOrderService.deleteCheOrderByIds(ids));
    }
   //获取服务项目
    @GetMapping(value = "/serviceItem")
    public Result getServiceItems(Long sid){
        Map map = new HashMap();
        map.put("sid",sid);
        Map<Object, Object> map1 = cheOrderService.selectAllByOrderId(map);
        return Result.success(map1);
    }
}
